// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/init/init.h>
#include <turbo/flags/flag.h>
#include <gtest/gtest.h>

#include <pollux/testing/exec/fuzzer_util.h>
#include <pollux/testing/exec/join_fuzzer_runner.h>

TURBO_FLAG(int64_t,
    seed,
    0,
    "Initial seed for random number generator used to reproduce previous "
    "results (0 means start with random seed).");

TURBO_FLAG(std::string,
    presto_url,
    "",
    "Presto coordinator URI along with port. If set, we use Presto "
    "source of truth. Otherwise, use DuckDB. Example: "
    "--presto_url=http://127.0.0.1:8080");

TURBO_FLAG(uint32_t,
    req_timeout_ms,
    1000,
    "Timeout in milliseconds for HTTP requests made to reference DB, "
    "such as Presto. Example: --req_timeout_ms=2000");

TURBO_FLAG(int64_t,allocator_capacity, 8L << 30, "Allocator capacity in bytes.");

TURBO_FLAG(int64_t,arbitrator_capacity, 6L << 30, "Arbitrator capacity in bytes.");

using namespace kumo::pollux::exec;

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);

  // Calls common init functions in the necessary order, initializing
  // singletons, installing proper signal handlers for better debugging
  // experience.
  melon::Init init(&argc, &argv);
  test::setupMemory(FLAGS_allocator_capacity, FLAGS_arbitrator_capacity);
  std::shared_ptr<kumo::pollux::memory::MemoryPool> rootPool{
      kumo::pollux::memory::memoryManager()->addRootPool()};
  auto referenceQueryRunner = test::setupReferenceQueryRunner(
      rootPool.get(), FLAGS_presto_url, "join_fuzzer", FLAGS_req_timeout_ms);
  const size_t initialSeed = FLAGS_seed == 0 ? std::time(nullptr) : FLAGS_seed;
  return test::JoinFuzzerRunner::run(
      initialSeed, std::move(referenceQueryRunner));
}
